#!/bin/sh
# shellcheck shell=dash disable=SC2169,SC3010,SC3036,SC3015 source=/dev/null
#
# Startup script to check if /usr/local partition (deedbeef-03) might not
# occupy all space on the disk and if so we resize it accordingly.
#

check_userfs_resize() {

  # source all data from /var/hm_mode
  [[ -r /var/hm_mode ]] && . /var/hm_mode

  # remove obsolete /usr/local/.doResizeLocalFS
  [[ -f "/usr/local/.doResizeLocalFS" ]] && rm -f /usr/local/.doResizeLocalFS

  # skip execution on OCI or LXC-based systems
  if [[ ! "${HM_HOST}" =~ oci\|lxc ]]; then

    # extract partition number and device name
    DEVNODE=$(mountpoint -d /usr/local)
    DEVNAME=$(mountpoint -n /usr/local | cut -d' ' -f1)
    PARTNUM=$(echo -n "${DEVNAME}" | tail -c 1)
    DEV="/dev/$(readlink "/sys/dev/block/${DEVNODE}" | awk -F/ '{print $(NF-1)}')"

    # find out maximum size (in cylinders)
    START_CHS=$(/usr/sbin/parted -s -f "${DEV}" unit chs print | grep "^ ${PARTNUM} " | awk '{print $2}')
    END_CHS=$(/usr/sbin/parted -s -f "${DEV}" unit chs print | grep "^ ${PARTNUM} " | awk '{print $3}')
    MAX_CHS=$(/usr/sbin/parted -s -f "${DEV}" unit chs print | grep "Disk ${DEV}" | cut -d' ' -f3)

    # only resize the partition if END_CHS != MAX_CHS
    if [[ -n "${START_CHS}" && -n "${END_CHS}" && -n "${MAX_CHS}" && "${END_CHS}" != "${MAX_CHS}" ]]; then
      echo -n "resizepart, "

      # check if we should do resizefs afterwards before we umount
      # the userfs
      if [[ ! -r /usr/local/.doFactoryReset ]]; then
        RESIZEFS=1
      else
        RESIZEFS=0
      fi

      # umount /usr/local to resize it
      umount -f /usr/local

      # use resizepart to resize the /usr/local partition to the maximum size of
      # the disk
      /usr/sbin/parted -s -f "${DEV}" resizepart "${PARTNUM}" 100%

      # force PARTUUID to 0xDEEDBEEF because parted changes partuuid
      echo -en '\xEF\xBE\xED\xDE' | dd of="${DEV}" conv=notrunc bs=1 seek=$((0x1B8)) 2>/dev/null

      # check if we need to resize the filesystem as well or if we skip it
      # since a factory reset is anyway done in the next step
      if [[ ${RESIZEFS} -eq 1 ]]; then
        echo -n "resizefs, "

        # check the filesystem first
        /sbin/e2fsck -pDf "${DEVNAME}" >/dev/null 2>&1

        # resize userfs
        /sbin/resize2fs "${DEVNAME}" >/dev/null 2>&1
      fi

      # mount /usr/local again
      mount /usr/local

      return 0
    fi
  fi

  return 1
}

start() {
  echo -n "Checking for UserFS Resize: "

  if check_userfs_resize; then
    echo "OK"
  else
    echo "not required"
  fi
}

restart() {
  start
}

case "$1" in
  start)
    start
  ;;
  stop)
    # nothing
  ;;
  restart|reload)
    restart
  ;;
  *)
    echo "Usage: $0 {start|stop|restart}"
    exit 1
esac

exit 0
